package com.gsk.europepharma.salesReport.struts.actions;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRCartoContourDAO;
import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;
import com.gsk.europepharma.salesReport.utils.CreateCarto;
import com.gsk.europepharma.salesReport.utils.Legende;
import com.gsk.europepharma.salesReport.utils.UtilsSalesReport;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

/**
 * Affichage de la carto de droite. 
 */
public class CartoSecteur extends Action {
	
	private final static int WIDTH = 500;
	private final static int HEIGHT = 500;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Variables de session
		HttpSession sessionObject = request.getSession();
		Integer cs_adm_pdt = (Integer) sessionObject.getAttribute("cs_adm_pdt");
		Integer cs_adm_rgpt = (Integer) sessionObject.getAttribute("cs_adm_rgpt");
		Integer cs_adm_prstn = (Integer) sessionObject.getAttribute("cs_adm_prstn");
		Integer cs_network = (Integer) sessionObject.getAttribute("cs_network");
		Integer cs_month = (Integer) sessionObject.getAttribute("cs_month");
		// Fin de session
				
		String libGeogCartoS = "A COMPLETER";
		String idGeogCartoS = "";
		String nivGeogCartoS = "";

		tbSpBoSRCartoContourDAO cartoDAO = new tbSpBoSRCartoContourDAO();
		Session session = cartoDAO.getSession();
		Query req;
		String strReq = "";

		// Recuperation des parametres
		String idGeog = request.getParameter("idGeog");
		Integer idGeogInteger = Integer.parseInt(idGeog);
		String nivGeog = request.getParameter("nivGeog"); 
		String typeImage = request.getParameter("typeImage"); 
		
		String calculPalettes = request.getParameter("calculPalettes");
		
		String source = request.getParameter("source");
		Integer CS_SRC = Integer.parseInt(source);
		String unite = request.getParameter("unite");
		String periode = request.getParameter("periode");
		
		String indicateur = request.getParameter("indicateur");
		String indicateur1 = request.getParameter("indicateur1");
		String indicateur2 = request.getParameter("indicateur2");
		
		String x = request.getParameter("x");
		Integer xInteger = Integer.parseInt(x);
		
		String y = request.getParameter("y");
		Integer yInteger = Integer.parseInt(y);
		
		if (!indicateur.equals("")) { // HIERARCHIQUE 
		
			Integer xA0 = -1;
			Integer yA0 = -1;
			Integer idUGA = -1;
			if (xInteger != -1 && yInteger != -1) {
				// Parametres de session 
				Integer xMinCarte1 = (Integer) sessionObject.getAttribute("xMinCarte1");
				Integer yMinCarte1 = (Integer) sessionObject.getAttribute("yMinCarte1");
				Integer maxCarte1 = (Integer) sessionObject.getAttribute("maxCarte1");
				
				xA0 = xMinCarte1 + xInteger * maxCarte1 / WIDTH;
				yA0 = yMinCarte1 + yInteger * maxCarte1 / HEIGHT;
				// Recuperation  de l'uga
				tbSpBoSRCartoContourDAO pixelDAO = new tbSpBoSRCartoContourDAO();
				session = pixelDAO.getSession();
				strReq = "";
				strReq = pixelDAO.QUERY_CLICK_UGA;
				req = session.getNamedQuery(strReq);
				req.setInteger("pos_x", xA0);
				req.setInteger("pos_y", yA0);
				req.setInteger("cs_network", cs_network);
				
				String nivGeogCartoR = (String) sessionObject.getAttribute("nivGeogCartoR");
				String idGeogCartoR = (String) sessionObject.getAttribute("idGeogCartoR");
				
				req.setInteger("idGeog", Integer.parseInt(idGeogCartoR)); 
				req.setString("nivGeog", nivGeogCartoR); 
				
				ArrayList result1 = new ArrayList(req.list());
				Iterator iter1 = result1.iterator();
				idUGA = -2;
				while (iter1.hasNext()) {
					idUGA =  Integer.parseInt(iter1.next().toString());
				}
			}
	
			if (nivGeog.equals("Zone")) {
				strReq = cartoDAO.QUERY_DESSINE_CARTE2_REGION;
			} else {
				strReq = cartoDAO.QUERY_DESSINE_CARTE2_SECTEUR;
			}
			
			req = session.getNamedQuery(strReq);
			req.setInteger("cs_network", cs_network);
			if (xA0 == -1) {
				req.setInteger("idGeog", idGeogInteger);
				req.setString("nivGeog", nivGeog);
			} else {
				req.setInteger("idGeog", idUGA);
				req.setString("nivGeog", "UGA");
			}
			ArrayList result = new ArrayList(req.list());
	
			Iterator iter = result.iterator();
	
			JSONArray jsonArrayPixels = new JSONArray();
	
			Integer xmin = 4000;
			Integer xmax = 0;
			Integer ymin = 4000;
			Integer ymax = 0;
	
			while (iter.hasNext()) {
				Object[] tmp = (Object[]) iter.next();
				
				if (libGeogCartoS.equals("A COMPLETER")) {
					libGeogCartoS = tmp[4].toString();
					idGeogCartoS = tmp[3].toString();
					if (tmp[5].toString().equals("S")) {
						nivGeogCartoS = "Secteur";
					} else if (tmp[5].toString().equals("R")) {
						nivGeogCartoS = "Region";
					}
				}
				
				JSONObject pixel = new JSONObject(); 
				pixel.put("x", tmp[0]);
				pixel.put("y", tmp[1]);
				pixel.put("color", Integer.parseInt(tmp[2].toString()));
				jsonArrayPixels.add(pixel);
	
				xmin = Math.min(xmin, Integer.parseInt(tmp[0].toString()));
				xmax = Math.max(xmax, Integer.parseInt(tmp[0].toString()));
				ymin = Math.min(ymin, Integer.parseInt(tmp[1].toString()));
				ymax = Math.max(ymax, Integer.parseInt(tmp[1].toString()));
			}
	
			// Ensemble des points pour le remplissage
			if (nivGeog.equals("Zone")) {
				strReq = cartoDAO.QUERY_REMPLIR_CARTE2_REGION;
				req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());		
			} else {
				strReq = cartoDAO.QUERY_REMPLIR_CARTE2_SECTEUR;
				req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
			}
			
			req.setString("indicateur", indicateur);
			req.setString("data", unite + periode);	
			req.setInteger("cs_adm_pdt", cs_adm_pdt);
			req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
			req.setInteger("cs_adm_prstn", cs_adm_prstn);
			req.setInteger("CS_SRC", CS_SRC);
			req.setInteger("cs_month", cs_month);
			req.setInteger("cs_network", cs_network);
			if (xA0 == -1) {
				req.setInteger("idGeog", idGeogInteger);
				req.setString("nivGeog", nivGeog);
			} else {
				req.setInteger("idGeog", idUGA);
				req.setString("nivGeog", "UGA");
			}
	
			result = new ArrayList(req.list());
	
			iter = result.iterator();
	
			JSONArray jsonArrayPixelsPm = new JSONArray();
	
			while (iter.hasNext()) {
				Object[] tmp = (Object[]) iter.next();
				
				JSONObject pixel = new JSONObject(); 
				pixel.put("x", tmp[0]);
				pixel.put("y", tmp[1]);
				if (tmp[2] != null) {
					pixel.put("val", tmp[2]);
				} else {
					pixel.put("val", 0.0);
				}
				pixel.put("niveau", tmp[3]);
				jsonArrayPixelsPm.add(pixel);
			}
	
			// Si un recalcul de la legende est necessaire
			if (calculPalettes.equals("true")) {
				// Requete pour la legende 
				Legende legendeSecteur = (Legende) sessionObject.getAttribute("legendeSecteur");
				// Recupération de la legende Region
				Legende legendeRegion = (Legende) sessionObject.getAttribute("legendeRegion");
				Double minLegende2 = 0.0;
				Double maxLegende2 = 0.0;
				Integer count2 = 0;
				tbSpBoSRCartoContourDAO cartoContourDAO = new tbSpBoSRCartoContourDAO();
				session = cartoContourDAO.getSession();
				strReq = "";
				if (nivGeog.equals("Zone")) {
					strReq = cartoContourDAO.QUERY_BORNES_CARTE2_REGION;
				} else {
					strReq = cartoContourDAO.QUERY_BORNES_CARTE2_SECTEUR;
				}
				req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
				req.setString("indicateur", indicateur);
				req.setString("data", unite + periode);	
				req.setInteger("cs_adm_pdt", cs_adm_pdt);
				req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
				req.setInteger("cs_adm_prstn", cs_adm_prstn);
				req.setInteger("CS_SRC", CS_SRC);
				req.setInteger("cs_month", cs_month);
				req.setInteger("cs_network", cs_network);
				if (xA0 == -1) {
					req.setInteger("idGeog", idGeogInteger);
					req.setString("nivGeog", nivGeog);
				} else {
					req.setInteger("idGeog", idUGA);
					req.setString("nivGeog", "UGA");
				}
				req.setMaxResults(10);
				result = new ArrayList(req.list());
				iter = result.iterator();
				// Calcul du min, max et du nombre de valeurs pour la legende 
				while (iter.hasNext()) {
					Object[] val = (Object[]) iter.next();
					if (val[0] == null) {
						minLegende2 = null;
					} else {
						minLegende2 = Double.parseDouble(val[0].toString());
					}
					if (val[1] == null) {
						maxLegende2 = null;
					} else {
						maxLegende2 = Double.parseDouble(val[1].toString());
					}
					count2 = Integer.parseInt(val[2].toString());
				}
				
				if (minLegende2 != null && maxLegende2 != null) {
					
					// Nouveau calcul min, max, count 
					Double minLegende1 = minLegende2;
					Double maxLegende1 = maxLegende2;
					Integer count1 = count2;
					if (indicateur.equals("PM") || indicateur.equals("D_PM") || indicateur.equals("EVOL_VOL")) {
						cartoContourDAO = new tbSpBoSRCartoContourDAO();
						session = cartoContourDAO.getSession();
						strReq = cartoContourDAO.QUERY_BORNES_CARTE1;
						req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
						req.setString("indicateur", indicateur);
						req.setString("data", unite + periode);	
						req.setInteger("cs_adm_pdt", cs_adm_pdt);
						req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
						req.setInteger("cs_adm_prstn", cs_adm_prstn);
						req.setInteger("CS_SRC", CS_SRC);
						req.setInteger("cs_month", cs_month);
						req.setInteger("cs_network", cs_network);
						req.setString("nivGeog", nivGeog); 
						req.setInteger("idGeog", idGeogInteger);
						req.setMaxResults(10);
						result = new ArrayList(req.list());
						iter = result.iterator();
						while (iter.hasNext()) {
							Object[] val = (Object[]) iter.next();
							minLegende1 = Double.parseDouble(val[0].toString());
							maxLegende1 = Double.parseDouble(val[1].toString());
							count1 = Integer.parseInt(val[2].toString());
						}
					}
					// On prend les extremites des legendes des 2 cartes 
					Double minLegende = Math.min(minLegende1, minLegende2);
					Double maxLegende = Math.max(maxLegende1, maxLegende2);
					Integer count = Math.max(count1, count2);
					
					if (!legendeSecteur.getTypeClassement().equals("DU") || !legendeRegion.getTypeClassement().equals("DU")) {
						// Mise en session
						legendeSecteur.setMin(minLegende);
						legendeSecteur.setMax(maxLegende);
						legendeSecteur.setCount(count);
						
						// Initialisation  de la palette 
						double pas = (maxLegende - minLegende) / legendeSecteur.getNbTranches();
						double tempPas = minLegende;
						for (int i = 0; i < legendeSecteur.getNbTranches(); i++) {
							legendeSecteur.getPalettes().get(i).setMin(UtilsSalesReport.arrondir(tempPas, 3));
							legendeSecteur.getPalettes().get(i).setMax(UtilsSalesReport.arrondir(tempPas + pas, 3));
							tempPas += pas;
						}
						sessionObject.setAttribute("legendeSecteur", legendeSecteur);
						
						if (indicateur.equals("PM") || indicateur.equals("D_PM") || indicateur.equals("EVOL_VOL")) {
							legendeRegion.setMin(minLegende);
							legendeRegion.setMax(maxLegende);
							//legendeRegion.setCount(count);
							sessionObject.setAttribute("legendeRegion", legendeRegion);
						}
						
					}
				}
			}
	
			BufferedImage bufferedImage = CreateCarto.createCarto(jsonArrayPixels, xmin, ymin, xmax, ymax, jsonArrayPixelsPm, request, "Secteur");
	
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
	
			ImageIO.write(bufferedImage, typeImage, baos);
			
			String encodedImage = Base64.encode(baos.toByteArray());
	
			response.reset();
			response.setHeader("Content-Type", "image/" + typeImage);
	
			if (idUGA != -2) {
				response.getOutputStream().write(encodedImage.getBytes("UTF-8"));
				response.getOutputStream().flush();
			}
			
			// Mise en session 
	        if (libGeogCartoS != null && !libGeogCartoS.equals("") 
	        		&& idGeogCartoS != null && !idGeogCartoS.equals("") 
	        		&& nivGeogCartoS != null && !nivGeogCartoS.equals("")) {
	        	sessionObject.setAttribute("libGeogCartoS", libGeogCartoS);
	        	sessionObject.setAttribute("idGeogCartoS", idGeogCartoS);
	        	sessionObject.setAttribute("nivGeogCartoS", nivGeogCartoS);
	        }
	        
	        // Calcul du PM et deltaPM 
	        if (nivGeogCartoS != null && !nivGeogCartoS.equals("")) {
		        tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
				session = salesDAO.getSession();
				strReq = salesDAO.QUERY_RECUP_SALES;
				req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
				req.setString("data", unite + periode);	
				req.setInteger("cs_adm_pdt", cs_adm_pdt);
				req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
				req.setInteger("cs_adm_prstn", cs_adm_prstn);
				req.setInteger("CS_SRC", CS_SRC);
				req.setInteger("cs_network", cs_network);
				req.setInteger("cs_month", cs_month);
				req.setString("nivGeog", nivGeogCartoS); 
				req.setInteger("idGeog", Integer.parseInt(idGeogCartoS));
				
				sessionObject.setAttribute("idGeogCartoS", idGeogCartoS);
				
				req.setMaxResults(10);
				result = new ArrayList(req.list());
				iter = result.iterator();
				while (iter.hasNext()) {
					Object[] val = (Object[]) iter.next();
					String deltapm = UtilsSalesReport.getDeltaPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), Double.parseDouble(val[3].toString()), Double.parseDouble(val[4].toString()), 1) + "";
					sessionObject.setAttribute("deltapmGeogCartoS", deltapm);
					String pm = UtilsSalesReport.getPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[3].toString()), 1) + "";
					sessionObject.setAttribute("pmGeogCartoS", pm);
					String vol = UtilsSalesReport.getVol(Double.parseDouble(val[1].toString()), 0) + "";
					sessionObject.setAttribute("volGeogCartoS", vol);
					String deltavol = UtilsSalesReport.getDeltaVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 0) + "";
					sessionObject.setAttribute("deltavolGeogCartoS", deltavol);
					String evol = UtilsSalesReport.getEvolVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 1) + "";
					sessionObject.setAttribute("evolGeogCartoS", evol);
				}
	        }
	        
		} else { // ISOGEOG  
			
			// Si un recalcul de la legende est necessaire
			if (calculPalettes.equals("true")) {
				// Requete pour la legende 
				Legende legendeSecteur = (Legende) sessionObject.getAttribute("legendeSecteur");
				Double minLegende2 = 0.0;
				Double maxLegende2 = 0.0;
				Integer count2 = 0;
				tbSpBoSRCartoContourDAO cartoContourDAO = new tbSpBoSRCartoContourDAO();
				session = cartoContourDAO.getSession();
				strReq = "";
				if (nivGeog.equals("Zone") || nivGeog.equals("Region")) {
					strReq = cartoContourDAO.QUERY_BORNES_CARTE1;
				} else {
					strReq = cartoContourDAO.QUERY_BORNES_CARTE2_SECTEUR;
				}
				req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
				req.setString("indicateur", indicateur2);
				req.setString("data", unite + periode);	
				req.setInteger("cs_adm_pdt", cs_adm_pdt);
				req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
				req.setInteger("cs_adm_prstn", cs_adm_prstn);
				req.setInteger("CS_SRC", CS_SRC);
				req.setInteger("cs_month", cs_month);
				req.setInteger("cs_network", cs_network);
				req.setInteger("idGeog", idGeogInteger);
				req.setString("nivGeog", nivGeog);
				req.setMaxResults(10);
				ArrayList result = new ArrayList(req.list());
				Iterator iter = result.iterator();
				// Calcul du min, max et du nombre de valeurs pour la legende 
				while (iter.hasNext()) {
					Object[] val = (Object[]) iter.next();
					if (val[0] == null) {
						minLegende2 = null;
					} else {
						minLegende2 = Double.parseDouble(val[0].toString());
					}
					if (val[1] == null) {
						maxLegende2 = null;
					} else {
						maxLegende2 = Double.parseDouble(val[1].toString());
					}
					count2 = Integer.parseInt(val[2].toString());
				}
				
				// Mise en session
				legendeSecteur.setMin(minLegende2);
				legendeSecteur.setMax(maxLegende2);
				legendeSecteur.setCount(count2);
				
				// Initialisation  de la palette 
				double pas = (maxLegende2 - minLegende2) / legendeSecteur.getNbTranches();
				double tempPas = minLegende2;
				for (int i = 0; i < legendeSecteur.getNbTranches(); i++) {
					legendeSecteur.getPalettes().get(i).setMin(UtilsSalesReport.arrondir(tempPas, 3));
					legendeSecteur.getPalettes().get(i).setMax(UtilsSalesReport.arrondir(tempPas + pas, 3));
					tempPas += pas;
				}
				sessionObject.setAttribute("legendeSecteur", legendeSecteur);
			}
			
			String libGeogCartoR = "";
			String idGeogCartoR = "";
			String nivGeogCartoR = "";
			
			cartoDAO = new tbSpBoSRCartoContourDAO();
			session = cartoDAO.getSession();
			strReq = "";
			if (nivGeog.equals("Zone")) {
				strReq = cartoDAO.QUERY_DESSINE_CARTE1_ZONE;
			} else if (nivGeog.equals("Region")) {
				strReq = cartoDAO.QUERY_DESSINE_CARTE1_REGION;
			} else {
				strReq = cartoDAO.QUERY_DESSINE_CARTE2_SECTEUR;
			}
			req = session.getNamedQuery(strReq);
			req.setInteger("idGeog", idGeogInteger);
			req.setString("nivGeog", nivGeog);
			req.setInteger("cs_network", cs_network);
			
			ArrayList result = new ArrayList(req.list());
			Iterator iter = result.iterator();
			JSONArray jsonArrayPixels = new JSONArray();
			
			Integer xmin = 4000;
			Integer xmax = 0;
			Integer ymin = 4000;
			Integer ymax = 0;
			
			while (iter.hasNext()) {
				Object[] tmp = (Object[]) iter.next();
				JSONObject pixel = new JSONObject(); 
				pixel.put("x", tmp[0]);
				pixel.put("y", tmp[1]);
				pixel.put("color", Integer.parseInt(tmp[2].toString()));
				jsonArrayPixels.add(pixel);
				if (tmp[0] != null) {
					xmin = Math.min(xmin, Integer.parseInt(tmp[0].toString()));
					xmax = Math.max(xmax, Integer.parseInt(tmp[0].toString()));
				}
				if (tmp[1] != null) {
					ymin = Math.min(ymin, Integer.parseInt(tmp[1].toString()));
					ymax = Math.max(ymax, Integer.parseInt(tmp[1].toString()));
				}
				idGeogCartoR = tmp[3].toString();
				libGeogCartoR = tmp[4].toString();
				if (tmp[5].toString().equals("Z")) {
					nivGeogCartoR = "Zone";
				} else if (tmp[5].toString().equals("R")) {
					nivGeogCartoR = "Region";
				} else if (tmp[5].toString().equals("S")) {
					nivGeogCartoR = "Secteur";
				}
			}
			
			// Ensemble des points pour le remplissage
			cartoDAO = new tbSpBoSRCartoContourDAO();
			session = cartoDAO.getSession();
			if (nivGeog.equals("Zone") || nivGeog.equals("Region")) {
				strReq = cartoDAO.QUERY_REMPLIR_CARTE1;
			} else {
				strReq = cartoDAO.QUERY_REMPLIR_CARTE2_SECTEUR;
			}
			req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
			
			req.setString("indicateur", indicateur2);
			req.setString("data", unite + periode);	
			req.setInteger("cs_adm_pdt", cs_adm_pdt);
			req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
			req.setInteger("cs_adm_prstn", cs_adm_prstn);
			req.setInteger("CS_SRC", CS_SRC);
			req.setInteger("cs_month", cs_month);
			req.setInteger("cs_network", cs_network);
			req.setInteger("idGeog", idGeogInteger);
			req.setString("nivGeog", nivGeog);
			
			result = new ArrayList(req.list());
			iter = result.iterator();
			JSONArray jsonArrayPixelsPm = new JSONArray();
			
			while (iter.hasNext()) {
				Object[] tmp = (Object[]) iter.next();
				JSONObject pixel = new JSONObject(); 
				pixel.put("x", tmp[0]);
				pixel.put("y", tmp[1]);
				if (tmp[2] != null) {
					pixel.put("val", tmp[2]);
				} else {
					pixel.put("val", 0.0);  
				}
				pixel.put("niveau", tmp[3]);
				jsonArrayPixelsPm.add(pixel);
			}
			
			BufferedImage bufferedImage = CreateCarto.createCarto(jsonArrayPixels, xmin, ymin, xmax, ymax, jsonArrayPixelsPm, request, "Secteur");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(bufferedImage, typeImage, baos);
			String encodedImage = Base64.encode(baos.toByteArray());
	        response.reset();
	        response.setHeader("Content-Type", "image/" + typeImage);
	        response.getOutputStream().write(encodedImage.getBytes("UTF-8"));
	        response.getOutputStream().flush();
	        
	        // Mise en session 
	        if (idGeogCartoR != null && !idGeogCartoR.equals("") 
	        		&& nivGeogCartoR != null && !nivGeogCartoR.equals("")
	        		&& libGeogCartoR != null && !libGeogCartoR.equals("")) {
	        	sessionObject.setAttribute("libGeogCartoS", libGeogCartoR);
	        	sessionObject.setAttribute("idGeogCartoS", idGeogCartoR);
	        	sessionObject.setAttribute("nivGeogCartoS", nivGeogCartoR);
	        }
	        
	        // Calcul du PM et deltaPM 
	        tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
			session = salesDAO.getSession();
			strReq = salesDAO.QUERY_RECUP_SALES;
			req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
			req.setString("data", unite + periode);	
			req.setInteger("cs_adm_pdt", cs_adm_pdt);
			req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
			req.setInteger("cs_adm_prstn", cs_adm_prstn);
			req.setInteger("CS_SRC", CS_SRC);
			req.setInteger("cs_network", cs_network);
			req.setInteger("cs_month", cs_month);
			req.setString("nivGeog", nivGeogCartoR); 
			req.setInteger("idGeog", Integer.parseInt(idGeogCartoR));
			
			sessionObject.setAttribute("idGeogCartoS", idGeogCartoR);
			
			req.setMaxResults(10);
			result = new ArrayList(req.list());
			iter = result.iterator();
			while (iter.hasNext()) {
				Object[] val = (Object[]) iter.next();
				String deltapm = UtilsSalesReport.getDeltaPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), Double.parseDouble(val[3].toString()), Double.parseDouble(val[4].toString()), 1) + "";
				sessionObject.setAttribute("deltapmGeogCartoS", deltapm);
				String pm = UtilsSalesReport.getPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[3].toString()), 1) + "";
				sessionObject.setAttribute("pmGeogCartoS", pm);
				String vol = UtilsSalesReport.getVol(Double.parseDouble(val[1].toString()), 1) + "";
				sessionObject.setAttribute("volGeogCartoS", vol);
				String deltavol = UtilsSalesReport.getDeltaVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 1) + "";
				sessionObject.setAttribute("deltavolGeogCartoS", deltavol);
				String evol = UtilsSalesReport.getEvolVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 1) + "";
				sessionObject.setAttribute("evolGeogCartoS", evol);
			}
			
		}
		
		return null;
	}

}
